#!/usr/bin/perl
#
####  Note: this program is run by typing 'perl OS_X_SSD_TRIM_Hack.pl' in to the Terminal application
#
#   Mac OS X SSD TRIM Enable Hack
#
#   This demonstration tool is provided for educational purposes only. No warranty is provided.
#
#
use Cwd;
use Cwd 'abs_path';
#
print {STDOUT} ("Mac OS X SSD TRIM Enable HACK (v1.0)\n");
$cwd = getcwd();
if ($ARGV[0] =~ m/debug/ig) {       # User passed debug in command line argument
    $DEBUG = 1;     # We will print additional details of string search to STDOUT
    print {STDOUT} ("\t***  DEBUG enabled  ***\n");
    print {STDOUT} ("Current working directory is '$cwd'\n");
} else {
    $DEBUG = 0;
};

undef $/;       # Undefine end-of-line. Will read entire file into buffer
$rec = "";      # Initialize
$rslt = "";
$orig_rslt = "";
$count = 0;     # Count the number of times we locate the target string.  It should only be 1
#
####  Read System File  ####
#
$cwd = getcwd();
$file_target= '/System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage';
print {STDOUT} ("Open file IOAHCIBlockStorage:\n   '$file_target'\n");
open (Fin, $file_target);
$rec  = <Fin>;

close(Fin);
#
####  Create local backup copy  ####
#
open (Fsave,"> IOAHCIBlockStorage.backup");
print {Fsave} ($rec);
close (Fsave);
#
####  Locate record if already hacked  ####
#
$orig = $rec;   # Save original values
#
if ($rec =~ m/\n(\x00Solid State\x00Rotational\x00)(\0\0\0\0\0\0\0\0\0\0)(.*?)\n/sg) {
    ++$count;
    print {STDOUT} ("\nFound string '$1'.  'APPLE SSD' has already been replaced with nuls.\n");
    $rslt = substr($rec,  $-[0], $+[0] - $-[0]);
    $orig_rslt = substr($orig,  $-[0], $+[0] - $-[0]);
    if ($DEBUG == 1) {
        print {STDOUT} ("\nFound 1 at ", $-[1], " to ", $+[1], "'", $1, ", hex'", unpack(h90,$1), "'\n");
        print {STDOUT} ("\nFound 2 at ", $-[2], " to ", $+[2], "'", $2, ", hex'", unpack(h90,$2), "'\n");
        print {STDOUT} ("\nFound 3 at ", $-[3], " to ", $+[3], "'", $3, "'\n      hex'", unpack(h9999,$3), "'\n");
        print {STDOUT} ("\nFound full at ", $-[0], " to ", $+[0], "'", $rslt, "'\n      hex'", unpack(h9999,$rslt), "'\n");
    };
    $rslt =~ s/\0/_/g;
    $rslt =~ s/\n/./g;
    $rslt =~ s/[[:cntrl:]]/\$/g;
    print {STDOUT} ("\n   Record contents  (note:  _=nul, .=\\n, \$=control character)\n\t'", $rslt, "'\n");
    print {STDOUT} ("\nAborting.  No changes required.\n");
    if ($DEBUG != 1) {
        exit -1;
    };
};
#
####  Find and replace matching record  ####
#
while ($rec =~ s/\n(\x00Solid State\x00Rotational\x00)(APPLE SSD\x00)(.*?)\n/\n\1\0\0\0\0\0\0\0\0\0\0\3\n/sg) {
    ++$count;
    print {STDOUT} ("Found string.  Count = $count.\n");
    $rslt = substr($rec,  $-[0], $+[0] - $-[0]);
    $orig_rslt = substr($orig,  $-[0], $+[0] - $-[0]);
    if ($DEBUG == 1) {
        print {STDOUT} ("\nFound 1 at ", $-[1], " to ", $+[1], "'", $1, ", hex'", unpack(h90,$1), "'\n");
        print {STDOUT} ("\nFound 2 at ", $-[2], " to ", $+[2], "'", $2, ", hex'", unpack(h90,$2), "'\n");
        print {STDOUT} ("\nFound 3 at ", $-[3], " to ", $+[3], "'", $3, "'\n      hex'", unpack(h9999,$3), "'\n");
        print {STDOUT} ("\nFound full at ", $-[0], " to ", $+[0], "'", $rslt, "'\n      hex'", unpack(h9999,$rslt), "'\n");
    };
    $rslt =~ s/\0/_/g;
    $rslt =~ s/\n/./g;
    $rslt =~ s/[[:cntrl:]]/\$/g;
    $orig_rslt =~ s/\0/_/g;
    $orig_rslt =~ s/\n/./g;
    $orig_rslt =~ s/[[:cntrl:]]/\$/g;
    print {STDOUT} ("\n   Record contents (note: _=nul, .=\\n, \$=control character)\n\t'", $orig_rslt, "'\n\n   Updated to:\n\t'", $rslt, "'\n");
};
#
####  Make sure we have only seen one of these  ####
#
if (($count == 1) or ($DEBUG == 1)) {      # We found one and only one occurence of string
    print {STDOUT} ("\n\nSuccess.  Count = $count.  File updated and .new created.\n");
    $Fnew_name = 'IOAHCIBlockStorage.new';
    open(Fnew, "> $Fnew_name");
    print {Fnew} ($rec);
    close(Fnew);
    print {STDOUT} ("\nTo activate this hack.  The following commands need to be entered in Terminal using the SUDO command.\nCopy only the text between >>>>  and  <<<< into Terminal.\nYou will be prompted for your password.  (The Terminal application is located in the Applications / Utilies folder in your computer.\n");
    print {STDOUT} ("\n1) First step is to copy the existing file into the same directory with the '.original' appended to the name.\n\n\t>>>>  SUDO cp ", $file_target, " ", $file_target, ".original  <<<<\n",
    "\n2) Next step is to copy the modified file from your working directory to the destination.\n\n\t>>>>  SUDO cp ", $cwd,'/', $Fnew_name, " ", $file_target, "  <<<<\n");
    print {STDOUT} ("\n3) Final step is to change the modification date for system extensions that will cause the operating system to rebuild the system extensions follwing the next restart.\n\n\t>>>>  SUDO touch /System/Library/Extensions  <<<<\n");
    print {STDOUT} ("\nRestart your Mac to activate the SSD TRIM Enable hack.   Apple Menu -> Restart\n");
} else {
    print {STDOUT} ("\n\nERROR.  Count = $count.\n");
    exit -2;
};

print {STDOUT} ("\nComplete\n");